package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.dto.MallSearch;
import com.swlz.dto.OrderSearch;
import com.swlz.entity.MallOrder;
import com.swlz.entity.MallType;
import com.swlz.vo.OrderVO;
import com.swlz.vo.ProductVo;

import cn.hutool.core.util.StrUtil;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 商城订单
 * TableName : MALL_ORDER
 * ClassName : com.swlz.dao.MallOrderDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-29 02:24:56
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallOrderDAO {

	/**
	 * 添加一个商城订单
	 * 包含所有字段
	 * @param mallOrder - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(MallOrder mallOrder) throws SQLException {
		if(mallOrder.getOrderId()==null) {
			mallOrder.setOrderId(DBUtil.nextId());
		}
		return DBUtil.insert(mallOrder);
	}
 
	
	/**
	 * 添加一个商城订单
	 * 只包含非null的字段
	 * @param mallOrder - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(MallOrder mallOrder) throws SQLException {
		if(mallOrder.getOrderId()==null) {
			mallOrder.setOrderId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(mallOrder);
	}
	

	/**
	 * 根据主键更新一个商城订单
	 * 包含所有字段
	 * @param mallOrder - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(MallOrder mallOrder) throws SQLException {
		return DBUtil.update(mallOrder);
	}
	
	/**
	 * 根据主键更新一个商城订单
	 * 只包含非null的字段
	 * @param mallOrder - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(MallOrder mallOrder) throws SQLException {
		return DBUtil.updateSelective(mallOrder);
	}
	
	/**
	 * 根据主键删除一个商城订单
	 * @param orderId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long orderId) throws SQLException  {
		return DBUtil.execute("DELETE FROM MALL_ORDER WHERE ORDER_ID=?", orderId);
	}
	
	/**
	 * 根据主键获取一个商城订单
	 * @param orderId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallOrder selectById(Long orderId) throws SQLException  {
		return DBUtil.get("SELECT * FROM MALL_ORDER WHERE ORDER_ID=?", MallOrder.class ,orderId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param orderId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallOrder selectByModel(MallOrder mallOrder)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_ORDER WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallOrder,sql,params);
		return DBUtil.get(sql.toString(), MallOrder.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param orderId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public static  List<MallOrder> selectByModel(MallOrder mallOrder,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_ORDER WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallOrder,sql,params);
		sql.append(" ORDER BY ORDER_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), MallOrder.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param orderId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(MallOrder mallOrder)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM MALL_ORDER WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallOrder,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param orderId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private static void where(MallOrder mallOrder,StringBuffer sql,List<Object> params) {
		if(mallOrder==null){
			return;
		}

		if(mallOrder.getOrderId()!=null) {
			sql.append(" AND ORDER_ID=?");
			params.add(mallOrder.getOrderId());
		}

		if(mallOrder.getUserId()!=null) {
			sql.append(" AND USER_ID=?");
			params.add(mallOrder.getUserId());
		}

		if(mallOrder.getOrderAddressName()!=null) {
			sql.append(" AND ORDER_ADDRESS_NAME=?");
			params.add(mallOrder.getOrderAddressName());
		}

		if(mallOrder.getOrderAddressTel()!=null) {
			sql.append(" AND ORDER_ADDRESS_TEL=?");
			params.add(mallOrder.getOrderAddressTel());
		}

		if(mallOrder.getOrderAddressZip()!=null) {
			sql.append(" AND ORDER_ADDRESS_ZIP=?");
			params.add(mallOrder.getOrderAddressZip());
		}

		if(mallOrder.getOrderAddressCode()!=null) {
			sql.append(" AND ORDER_ADDRESS_CODE=?");
			params.add(mallOrder.getOrderAddressCode());
		}

		if(mallOrder.getOrderAddressInfo()!=null) {
			sql.append(" AND ORDER_ADDRESS_INFO=?");
			params.add(mallOrder.getOrderAddressInfo());
		}

		if(mallOrder.getOrderTime()!=null) {
			sql.append(" AND ORDER_TIME=?");
			params.add(mallOrder.getOrderTime());
		}

		if(mallOrder.getOrderMoney()!=null) {
			sql.append(" AND ORDER_MONEY=?");
			params.add(mallOrder.getOrderMoney());
		}

		if(mallOrder.getOrderMemo()!=null) {
			sql.append(" AND ORDER_MEMO=?");
			params.add(mallOrder.getOrderMemo());
		}

		if(mallOrder.getOrderPayTypeId()!=null) {
			sql.append(" AND ORDER_PAY_TYPE_ID=?");
			params.add(mallOrder.getOrderPayTypeId());
		}

		if(mallOrder.getOrderPayTime()!=null) {
			sql.append(" AND ORDER_PAY_TIME=?");
			params.add(mallOrder.getOrderPayTime());
		}

		if(mallOrder.getOrderPayId()!=null) {
			sql.append(" AND ORDER_PAY_ID=?");
			params.add(mallOrder.getOrderPayId());
		}

		if(mallOrder.getOrderTrackingUserId()!=null) {
			sql.append(" AND ORDER_TRACKING_USER_ID=?");
			params.add(mallOrder.getOrderTrackingUserId());
		}

		if(mallOrder.getOrderTrackingTime()!=null) {
			sql.append(" AND ORDER_TRACKING_TIME=?");
			params.add(mallOrder.getOrderTrackingTime());
		}

		if(mallOrder.getOrderTrackingMemo()!=null) {
			sql.append(" AND ORDER_TRACKING_MEMO=?");
			params.add(mallOrder.getOrderTrackingMemo());
		}

		if(mallOrder.getOrderTrackingTypeId()!=null) {
			sql.append(" AND ORDER_TRACKING_TYPE_ID=?");
			params.add(mallOrder.getOrderTrackingTypeId());
		}

		if(mallOrder.getOrderTrackingId()!=null) {
			sql.append(" AND ORDER_TRACKING_ID=?");
			params.add(mallOrder.getOrderTrackingId());
		}

		if(mallOrder.getOrderTrackingMoney()!=null) {
			sql.append(" AND ORDER_TRACKING_MONEY=?");
			params.add(mallOrder.getOrderTrackingMoney());
		}

		if(mallOrder.getOrderCheckTime()!=null) {
			sql.append(" AND ORDER_CHECK_TIME=?");
			params.add(mallOrder.getOrderCheckTime());
		}

		if(mallOrder.getOrderStatus()!=null) {
			sql.append(" AND ORDER_STATUS=?");
			params.add(mallOrder.getOrderStatus());
		}

	}
	
	public static long count(OrderSearch search)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM mall_order a INNER JOIN mall_order_detail b ON a.ORDER_ID = b.ORDER_ID");
		List<Object> params = new ArrayList<Object>();
		where(search,sql,params);
		System.out.println("SQL:"+sql);//日志
		System.out.println("patams:"+params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	public static List<OrderVO> select(OrderSearch search,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT a.*,b.PRODUCT_ID,b.ORDER_DETAIL_NUM,b.ORDER_DETAIL_ID,c.PRODUCT_NAME  FROM mall_order a INNER JOIN mall_order_detail b INNER JOIN mall_product c ON a.ORDER_ID = b.ORDER_ID and b.PRODUCT_ID =c.PRODUCT_ID");
		List<Object> params = new ArrayList<Object>();
		where(search,sql,params);
		sql.append(" ORDER BY "+search.getSortName()+" "+search.getSortOrder()+" LIMIT ?,?");
		params.add(index);
		params.add(len);
		System.out.println("SQL:"+sql);//日志
		System.out.println("patams:"+params);
		return DBUtil.list(sql.toString(), OrderVO.class, params.toArray());
	}
	
	private static void where(OrderSearch orderSearch,StringBuffer sql,List<Object> params)throws SQLException {
		if(orderSearch.getORDER_STATUS()!=null){
			sql.append(" AND a.ORDER_STATUS=?");
			params.add(orderSearch.getORDER_STATUS());
			}
		if(!StrUtil.hasBlank(orderSearch.getKey())){
			sql.append(" and (a.ORDER_ADDRESS_NAME like CONCAT('%',?,'%')");
			params.add(orderSearch.getKey());
			params.add(orderSearch.getKey());
		}
	}

	
}